/*
 * Copyright 2009-2009 Tanesha Project. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *    	 this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *    	 this list of conditions and the following disclaimer in the documentation 
 *    	 and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those 
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the Tanesha Project.
 */
package com.google.api.chart;

import com.google.api.chart.encoding.JoinHelper;
import com.google.api.chart.encoding.SimpleURLEncoding;

public final class AxisMaker {

	public static AxisMaker bottomx() {
		return new AxisMaker("x");
	}
	public static AxisMaker topx() {
		return new AxisMaker("t");
	}
	public static AxisMaker lefty() {
		return new AxisMaker("y");
	}
	public static AxisMaker righty() {
		return new AxisMaker("r");
	}
	
	public static AxisStyleMaker styleMaker() {
		return AxisStyleMaker.style();
	}

	private String axis;
	private String[] labels = null;
	private double[] positions = null;
	private Double rangeStart = null;
	private Double rangeEnd = null;
	private Double rangeInterval = null;
	private AxisStyleMaker style;
	private Double tickmarkLength = null;
	
	private AxisMaker(String axis) {
		this.axis = axis;
	}

	public AxisMaker labels(String... labels) {
		this.labels  = JoinHelper.map(labels, String.class, SimpleURLEncoding.encodeList);
		return this;
	}

	public AxisMaker labels(double[] positions, String... labels) {
		if (positions.length != labels.length)
			throw new RuntimeException("Usage error, positions.length not equal labels.length");
		labels(labels);
		this.positions  = positions;
		return this;
	}
	
	public AxisMaker positions(double... positions) {
		this.positions  = positions;
		return this;
	}
	
	public AxisMaker style(AxisStyleMaker style) {
		this.style  = style;
		return this;
	}

	public AxisMaker range(double start, double end) {
		this.rangeStart = start;
		this.rangeEnd = end;
		return this;
	}

	public AxisMaker range(double start, double end, double interval) {
		this.rangeStart = start;
		this.rangeEnd = end;
		this.rangeInterval = interval;
		return this;
	}
	
	public AxisMaker tickmarkLength(double length) {
		this.tickmarkLength = length;
		return this;
	}
	
	protected String positionToString(int idx) {
		if (positions == null)
			return null;
		StringBuilder buf = new StringBuilder(String.valueOf(idx));
		for (int i = 0; i < positions.length; i++) {
			buf.append(",").append(JoinHelper.doubleFormat(positions[i]));
		}
		return buf.toString();
	}
	
	protected String labelToString(int idx) {
		if (labels == null)
			return null;
		return idx + ":|" + JoinHelper.join(labels, "|");
	}
	
	protected String stylesToString(int idx) {
		if (style == null)
			return null;
		return idx + "," + style.toString();
	}
	
	protected String rangeToString(int idx) {
		if (rangeStart == null && rangeEnd == null) {
			return null;
		}
		return idx + "," + JoinHelper.doubleFormat(rangeStart) + "," + JoinHelper.doubleFormat(rangeEnd) + (rangeInterval == null ? "" : "," + JoinHelper.doubleFormat(rangeInterval));
	}

	protected String stylesTickmarkToString(final int idx) {
		if (tickmarkLength == null)
			return null;
		else
			return idx + "," + JoinHelper.doubleFormat(tickmarkLength);
	}

	@Override
	public String toString() {
		return axis;
	}
}
